package org.pointex.controller;

import java.io.IOException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.pointex.common.controller.BaseController;
import org.pointex.common.model.ParamObject;
import org.pointex.common.util.MD5Util;
import org.pointex.util.RSAUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

/**
 * 验证签名
 */
@Controller
@RequestMapping("/signCheck")
public class SignCheckController extends BaseController {
	

	/**
	 * 验签得到公钥 返回值 code:{ 0 失败 -1 参数值为空  1 成功} sign_e sign_m
	 * 
	 * @param uid
	 *            用户id
	 * @param name
	 *            公司名
	 * @param sign
	 *            签名
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/publicKey.do", produces = "text/plain;charset=UTF-8")
	public void getPublicKey(
			@RequestParam(value = "uid", required = true) String uid,
			@RequestParam(value = "name", required = true) String name,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		JSONObject json = new JSONObject();
		Integer flag = 0;
		try {
			if (isNuOrEmpty(uid, name)) {
				flag = -1;
			} else {
				ParamObject para=new ParamObject();
				para.setUid(uid);
				para.setName(name);
				para.setSign(MD5Util.getSign(para));
				//加密
				RSAUtils.encryPtion(para);
				flag=1;
				json.put("param", para);
			}
			json.put("code", flag);
			feedback(response, json.toString());
		} catch (Exception e) {
			logger.info("验签异常", e.getMessage());
			feedback(response, toJsr(json));
			e.printStackTrace();
			throw new RuntimeException("验签异常", e);
		} finally {
			try {
				json.put("code", flag);
				feedback(response, toJsr(json));
			} catch (Exception e) {
				throw new RuntimeException("网络繁忙，请稍后再试", e);
			}
		}
	}

}
